﻿using IdentityModel;
using IdentityServer4.AspNetIdentity;
using IdentityServer4.Extensions;
using IdentityServer4.Models;
using IdentityServer4.Services;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Security.Policy;
using System.Threading.Tasks;
using ZFY.Core;
using ZFY.UMC.Identity.Model;
using ZFY.UMC.API.Model;

namespace ZFY.UMC.Identity
{

    public class ZFYUserClaimsPrincipalFactory : UserClaimsPrincipalFactory<User>
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private  CurrentUserProfile _currentUserProfile;

        public ZFYUserClaimsPrincipalFactory(UserManager<User> userManager, IOptions<IdentityOptions> optionsAccessor,IHttpContextAccessor httpContextAccessor,CurrentUserProfile currentUserProfile) : base(userManager, optionsAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
            _currentUserProfile = currentUserProfile;
        }

        protected override async Task<ClaimsIdentity> GenerateClaimsAsync(User user)
        {
            var claimsIdentity = await base.GenerateClaimsAsync(user);
            // var claims = claimsIdentity.Claims.Where(x => ZFYProfileClaimTypes.All.Contains(x.Type));


            Console.Out.WriteLine(_httpContextAccessor.HttpContext?.Request.Host);
            
            claimsIdentity.AddClaimIfNotNull(ZFYProfileClaimTypes.NickName, user.NickName);
            claimsIdentity.AddClaimIfNotNull(ZFYProfileClaimTypes.Avatar, _httpContextAccessor.HttpContext.GetIdentityServerHost()+"/" +user.Avatar);
            claimsIdentity.AddClaimIfNotNull(ZFYProfileClaimTypes.Birthday, user.Birthday?.ToString("yyyy-MM-dd"));
            claimsIdentity.AddClaimIfNotNull(ZFYProfileClaimTypes.Signature, user.Signature);

            var address = string.Empty;

            if (user.Address != null)
            {
                
                address = $"{user.Address?.Province ?? string.Empty} {user.Address?.City ?? string.Empty} {user.Address?.County ?? string.Empty}";

                 claimsIdentity.AddClaim(new Claim(ZFYProfileClaimTypes.Address, address));
            }

            return claimsIdentity;
        }
    }

    public static class ClaimsIdentityExtension
    {
        public static void AddClaimIfNotNull(this ClaimsIdentity claimsIdentity,string type,string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                claimsIdentity.AddClaim(new Claim(type, value));
            }
        }
    }
    public static class HttpContextExtension
    {
        public static string GetRequestUrl(this HttpContext httpContext)
        {
            if (httpContext is null)
            {
                return null;
            }

            return $"{(httpContext.Request.IsHttps ? "https" : "http")}://{httpContext.Request.Host}";
        }
    }
    public class ZFYProfileService : ProfileService<User>
    {


        public ZFYProfileService(UserManager<User> userManager, IUserClaimsPrincipalFactory<User> claimsFactory, ILogger<ProfileService<User>> logger) : base(userManager, claimsFactory, logger)
        {
        }

        public override async Task GetProfileDataAsync(ProfileDataRequestContext context)
        {
            
            await base.GetProfileDataAsync(context);


        }
    }
}
